package com.youboy.wechatapi.utils.curd;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

/**
 * 
 * @author wh
 *
 * @param <PK>
 * @param <T>
 */
public abstract class MybatisRepositorySupport<PK, T> extends SqlSessionDaoSupport {

	/**
	 * 
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T get(PK id) {
			return (T) getSqlSession().selectOne(getNamespace() + ".get", id);
	}

	/**
	 * 
	 * @param entity
	 */
	public void save(T entity) {

			getSqlSession().insert(getNamespace() + ".save", entity);
	}

	/**
	 * 
	 * @param entity
	 */
	public void update(T entity) {

			getSqlSession().update(getNamespace() + ".update", entity);
	}

	/**
	 * 
	 * @param entity
	 */
	public void delete(T entity) {

			getSqlSession().delete(getNamespace() + ".delete", entity);
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	public List<T> query(Object value) {

			return getSqlSession().selectList(getNamespace() + ".query", value);
	}

	/**
	 * 
	 * @return
	 */
	public long getTotalCount(List<T> result) {
		return result.size();
	}
	protected abstract String getNamespace();
}
